package net.zjitc.arry;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SpareMat {
    private int row;
    private int col;
    private int count;
    private List<Sparse> list=new ArrayList<>();

    public SpareMat() {
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List<Sparse> getList() {
        return list;
    }

    public void setList(List<Sparse> list) {
        this.list = list;
    }

    public void createSpare(int[][] arr) {
        row = arr.length;
        col = arr[0].length;

//给一个矩阵 对其进行压缩存储
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                if (arr[i][j] != 0) {
                    list.add(new Sparse(i, j, arr[i][j]));
                    count++;
                }
            }
        }


    }

    public int[][] decompression() {
        int[][] temp=new int[row][col];

        //迭代
        Iterator iterator=list.iterator();
        while (iterator.hasNext()){
            Sparse sparse= (Sparse) iterator.next();
            temp[sparse.getRow()][sparse.getCol()]=sparse.getValue();
        }
        return temp;

    }
}
